package cn.com.bluemoon.order.print.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

import org.xhtmlrenderer.pdf.ITextRenderer;


public class FlyingSaucer2PDF {

	public static final String HTML = "html/zto180_rm.html";
	public static final String PDF = "d:\\Tested.pdf";

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FlyingSaucer2PDF f = new FlyingSaucer2PDF();
		try {
			f.printPdf();
			f.print(null);
		} catch (com.lowagie.text.DocumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PrintException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void printPdf() throws IOException, com.lowagie.text.DocumentException {
		String url = new File(HTML).toURI().toURL().toString();
		OutputStream os = new FileOutputStream(PDF);

		ITextRenderer renderer = new ITextRenderer();
		renderer.setDocument(url);
		renderer.layout();
		renderer.createPDF(os);

		os.close();
	}

	public void print(File file) throws FileNotFoundException, PrintException {
		// PrinterService ps = new PrinterService();
		// get the printer service by printer name
		PrintService pss = PrintServiceLookup.lookupDefaultPrintService();// ps.getCheckPrintService("Samsung
																			// ML-2850
																			// Series
																			// PCL6
																			// Class
																			// Driver");
		System.out.println("Printer - " + pss.getName());
		DocPrintJob job = pss.createPrintJob();
		DocAttributeSet das = new HashDocAttributeSet();
		Doc document = new SimpleDoc(new FileInputStream(new File(PDF)), DocFlavor.INPUT_STREAM.AUTOSENSE, das);
		// new htmldo
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
		job.print(document, pras);
	}
}